#!/usr/local/python/bin/python
# coding=utf-8
from tools.Util import Util

__author__ = 'Junjie.M'


class ExpInfo():
    _data = []

    def setData(self, data):
        global _data
        _data = data

    # 导数编码
    def getExpCode(self):
        return _data['expCode']

    # 导数名称
    def getExpName(self):
        return _data['expName']

    # 库名
    def getDbName(self):
        return _data['dbName']

    # 表名
    def getTbName(self):
        return _data['tbName']

    # 分隔符
    def getColSeprator(self):
        colSeprator = _data['colSeprator']
        if colSeprator.startswith('\\'):
            return '\\' + colSeprator
        return colSeprator

    # 分隔符
    def getColSeprator2(self):
        colSeprator = _data['colSeprator']
        return colSeprator

    # 导数路径
    def getExpPath(self):
        return _data['expPath']

    # 导数字段
    def getExpColums(self):
        colSeprator = self.getColSeprator()
        expColums = _data['expColums']
        expColums = expColums.replace('\\s', '')
        expColumArr = expColums.strip().split(',')
        expColums = 'CONCAT_WS(\'' + colSeprator + '\','
        count = 0
        for column in expColumArr:
            if count == 0:
                expColums += 'NVL(CAST(' + column + ' AS STRING),\'\')'
            else:
                expColums += ',NVL(CAST(' + column + ' AS STRING),\'\')'
            count += 1
        expColums += ')'
        return expColums

    # 导数字段
    def getExpColums2(self):
        expColums = _data['expColums']
        expColums = expColums.replace('\\s', '')
        expColums = expColums.replace('`','\`')
        return expColums

    # 导数字段
    def getExpColums3(self):
        expColums = _data['expColums']
        expColums = expColums.replace('\\s', '')
        expColumArr = expColums.strip().split(',')
        expColums = ''
        count = 0
        for column in expColumArr:
            if count == 0:
                expColums += 'NVL(CAST(' + column + ' AS STRING),\'\')'
            else:
                expColums += ',NVL(CAST(' + column + ' AS STRING),\'\')'
            count += 1
        return expColums

    # 表头字段
    def getHeader(self):
        colSeprator = _data['colSeprator']
        colSeprator = Util.decode(colSeprator)
        expColums = _data['expColums']
        expColums = expColums.replace('\\s', '')
        expColumArr = expColums.strip().split(',')
        header = ''
        count = 0
        for column in expColumArr:
            if count == 0:
                header += column
            else:
                header += colSeprator + column
            count += 1
        return header

    # where条件
    def getExpFilter(self):
        if not _data['expFilter']:
            return ''
        return 'WHERE ' + _data['expFilter'].replace('`', '\`')

    # 导数文件编码
    def getExpEncoding(self):
        return _data['expEncoding']

    # 是否生成flg文件
    def isNeedFlg(self):
        return _data['needFlg'] == '1'

    # 导数模式
    def getQueryType(self):
        return _data['queryType']

    # 自定义select语句
    def getStatement(self):
        statement = _data['statement']
        statement = statement.replace('`', '\`')
        statement = statement.replace('\\', '\\\\')
        statement = statement.replace('\"', '\\\"')
        return statement

    # 是否打印表头
    def isPrintHeader(self):
        return _data['printHeader'] == '1'
